/**
 * Copyright (C) © 2014 深圳市掌玩网络技术有限公司
 */


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * <p>
 * <strong>Features draft description.主要功能介绍</strong>
 * </p>
 * 
 * @author Administrator
 * @Create at 2015-11-26 上午11:20:34
 * @Version 1.0
 * 
 */
public class IOUtil {
    /**
     * 指针移动有点的问题.
     * @param is
     * @param bs
     * @param start
     * @param end
     * @return
     * @throws IOException
     */
    @Deprecated 
    public static boolean readAll(InputStream is, byte[] bs, int start, int end) throws IOException {
        while (start < end) {
            int bytesRead;
            bytesRead = is.read(bs, start, end - start);
            if (bytesRead < 0) {
                return false;
            }
            start += bytesRead;
        }
        return true;
    }

    public static int read(InputStream is, byte[] bs) throws IOException {
        int bytesRead;
        bytesRead = is.read(bs);
        return bytesRead;
    }

    public static void sendAll(OutputStream out, byte[] array) throws IOException {

        // send
        OutputStream output = out;
        output.write(array, 0, array.length);
        output.flush();

    }

    public static void send(OutputStream out, byte[] array, int start, int end) throws IOException {

        // send
        OutputStream output = out;
        output.write(array, 0, end-start);
        output.flush();

    }
    public static void send(DatagramSocket  from,InetAddress  toip,int toport, byte[] array,  int offset,int len) throws IOException {
        from.send(new DatagramPacket(array,offset,len, toip, toport));
    }
    public static void read(DatagramSocket  from,byte[] bs) throws IOException {
        DatagramPacket incoming = new DatagramPacket(bs, bs.length);
        from.receive(incoming); 
    }
}
